<?php
require_once "config.php";
class Database {

    private static $db;
    private $dbh;

    function __construct()
    {
        Config::load();
        $info = Config::$database;
        try{
            $this->dbh = new PDO(
                $info["driver"].":dbname=".$info["database"].";host=".$info["host"],
                $info["user"],
                $info["password"]
            );
        }catch (PDOException $ex){
            echo "Connection failed: " . $ex->getMessage();
            self::$db = null;
        }
    }


    public static function & getInstance(){
        if(!self::$db){
            self::$db = new Database();
        }
        return self::$db;
    }

    public function call($method, $args=array()){
        return call_user_func_array(array($this->dbh,$method), $args);
    }

    public static function prepare($sql) {
        $obj = self::getInstance()->dbh->prepare($sql);
        return $obj;
    }

}